package com.wenshi.app.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.wenshi.app.view.WsLoadMoreListView;
import com.wenshi.app.view.WsSwipeRefreshAndLoadMoreLayout;
import com.wenshi.view.IKeyValue;
import com.wenshi.view.WsViewTools;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/3/16.
 * 该类适用于分页加载的fragment即listview下拉刷新
 */
public abstract class WsReFreshAndLoadFragment extends  WsListViewFragment  implements WsSwipeRefreshAndLoadMoreLayout.IRefreshAndLoadMoreLisenter {
    private WsLoadMoreListView mLoadMoreListView;
    private Context mcontext;
    private WsSwipeRefreshAndLoadMoreLayout mRefreshAndLoadMoreView;
    private WsLoadMoreListView.OnListViewScrollListener onListViewScrollListener;

    private boolean hassetadapter = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mcontext=getActivity();

    }
    public void setOnListViewScrollListener(WsLoadMoreListView.OnListViewScrollListener onListViewScrollListener) {
        this.onListViewScrollListener = onListViewScrollListener;
    }
    protected void initLayOutAndListView()
    {
        Log.e("initLayOutAndListView", "initLayOutAndListView:1 "  );
        getWsWiewDelegate().setContentView(WsViewTools.getResource(mcontext,"wsview_activity_load_and_refresh","layout"));
//        mLoadMoreListView = (WsLoadMoreListView) findViewById(R.id.load_more_list);
        mLoadMoreListView = (WsLoadMoreListView) findViewById(WsViewTools.getResource(mcontext,"load_more_list","id"));
//        mRefreshAndLoadMoreView = (WsSwipeRefreshAndLoadMoreLayout) findViewById(R.id.refresh_and_load_more);
        mRefreshAndLoadMoreView = (WsSwipeRefreshAndLoadMoreLayout) findViewById(WsViewTools.getResource(mcontext,"refresh_and_load_more","id"));
    }
    public ListView getListView()
    {
        return mLoadMoreListView;
    }

    protected  void addAdapterData(ArrayList<IKeyValue> items )
    {
        adapter.addItemMore(items );
        whenloaddata(items,false);
    }
    protected void whenloaddata(ArrayList<IKeyValue> items, boolean isinit)
    {
        super.whenloaddata(items,isinit);

        if(items.size()==0)
        {
            mLoadMoreListView.setHaveMoreData(false,isinit);
        }else{
            mLoadMoreListView.setHaveMoreData(true,isinit);
        }
        mRefreshAndLoadMoreView.setRefreshing(false);
        mLoadMoreListView.onLoadComplete();
    }


}
